using UnityEngine;

public class ChargeUI : MonoBehaviour
{
	private GameObject uiobj;

	public UIEventListener moneybtn;

	public UIEventListener diamondbtn;

	public UIEventListener energybtn;

	public UIEventListener closebtn;

	public UISprite[] selectfors;

	public GameObject[] chargeitemobj;

	private int curitem;

	public UIEventListener[] moneyitems;

	public UIEventListener[] diamonditems;

	public UIEventListener[] energyitems;

	public UISprite[] adfrees;

	public UILabel energylabel;

	public UILabel timelabel;

	public UISprite limittimesflag;

	public UISprite scrollsp;

	public bool isenergy;

	public UIWidget item3wi;

	public UIAnchor item1an;

	public UIAnchor item2an;

	public UILabel spendlimitlabel;

	public int[] spendnum_limit = new int[3]
	{
		10,
		20,
		50
	};

	private int pretime;

	private int curtime;

	private bool calenergy;

	private void Start()
	{
		moneybtn.onClick = clickmoneyitem;
		diamondbtn.onClick = clickdiamonditem;
		energybtn.onClick = clickenergyitem;
		closebtn.onClick = clickclose;
		for (int i = 0; i < moneyitems.Length; i++)
		{
			moneyitems[i].onClick = OnClickbuymoneyBtn;
		}
		for (int j = 0; j < diamonditems.Length; j++)
		{
			diamonditems[j].onClick = OnClickbuydiamondBtn;
		}
		for (int k = 0; k < energyitems.Length; k++)
		{
			energyitems[k].onClick = OnClickbuyenergyBtn;
		}
	}

	private void Update()
	{
		if (isenergy && calenergy)
		{
			curtime = MenuUIController.instance.uplineui.caltime;
			if (pretime != curtime)
			{
				pretime = curtime;
				showtimefun(curtime);
			}
		}
	}

	public void showtimefun(int num)
	{
		if (MenuPlayerprefs.instance.GetEnergyrota() >= 1f)
		{
			timelabel.text = string.Empty;
			calenergy = false;
		}
		else
		{
			timelabel.text = "恢复时间 : " + MyMath.TimeSet(num);
		}
	}

	public void showenergynum()
	{
		energylabel.text = MenuPlayerprefs.instance.allenergy + "/" + MenuPlayerprefs.instance.maxenergy;
		scrollsp.fillAmount = MenuPlayerprefs.instance.GetEnergyrota();
		if (scrollsp.fillAmount < 1f)
		{
			calenergy = true;
		}
		else
		{
			calenergy = false;
			timelabel.text = string.Empty;
		}
		pretime = -1;
		if (MenuPlayerprefs.instance.energylimittimes < 3)
		{
			spendlimitlabel.text = string.Empty + spendnum_limit[MenuPlayerprefs.instance.energylimittimes];
		}
	}

	public void init_ui_info()
	{
		showselect(curitem);
		showenergynum();
		if (scrollsp.fillAmount < 1f)
		{
			calenergy = true;
			pretime = -1;
		}
		else
		{
			calenergy = false;
		}
		setAdfree();
	}

	public void setAdfree()
	{
		if (Platform.IsAdsFree())
		{
			for (int i = 0; i < adfrees.Length; i++)
			{
				adfrees[i].enabled = false;
			}
		}
	}

	public void OnClickbuyenergyBtn(GameObject btn)
	{
		int num = 0;
		while (true)
		{
			if (num < energyitems.Length)
			{
				if (btn.name.Equals("energy" + num))
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		buyenergy(num);
	}

	public void buyenergy(int num)
	{
		switch (num)
		{
			case 0:
				if (MenuPlayerprefs.instance.DecDiamond(1))
				{
					MenuPlayerprefs.instance.AddEnergy(1, late: true);
					showenergynum();
					MenuUIController.instance.enable_effectui(energyitems[0].transform.position, 2);
					Platform.flurryEvent_main(-2, 0);
				}
				else
				{
					showselect(1);
				}
				break;
			case 1:
				if (MenuPlayerprefs.instance.DecDiamond(5))
				{
					MenuPlayerprefs.instance.AddEnergy(5, late: true);
					showenergynum();
					MenuUIController.instance.enable_effectui(energyitems[1].transform.position, 2);
					Platform.flurryEvent_main(-2, 1);
				}
				else
				{
					showselect(1);
				}
				break;
			case 2:
				if (MenuPlayerprefs.instance.energylimittimes < 3)
				{
					if (MenuPlayerprefs.instance.DecDiamond(spendnum_limit[MenuPlayerprefs.instance.energylimittimes]))
					{
						MenuPlayerprefs.instance.SetMaxenergy(10, late: true);
						showenergynum();
						showselect(2);
						MenuUIController.instance.enable_effectui(energyitems[2].transform.position, 2);
						Platform.flurryEvent_main(-2, 2);
					}
					else
					{
						showselect(1);
					}
				}
				break;
		}
	}

	public void OnClickbuymoneyBtn(GameObject btn)
	{
		int num = 0;
		while (true)
		{
			if (num < moneyitems.Length)
			{
				if (btn.name.Equals("money" + num))
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		ChargeMoney(num);
	}

	public void ChargeMoney(int id)
	{
		// 视频
		AdManager.single.ShowVideo(delegate
		{
			if (id < 6)
			{
				MenuPlayerprefs.instance.AddMoney(Platform.coinCount[id]);
			}

		});
		//Platform.Billing(id);
	}

	public void OnClickbuydiamondBtn(GameObject btn)
	{
		int num = 0;
		while (true)
		{
			if (num < moneyitems.Length)
			{
				if (btn.name.Equals("diamond" + num))
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		ChargeDiamond(num + 6);
	}

	public void ChargeDiamond(int id)
	{
		// 视频
		AdManager.single.ShowVideo(delegate
		{
			if (id > 5 && id < 12)
			{
				MenuPlayerprefs.instance.AddDiamod(Platform.coinCount[id]);
			}

		});
		//Platform.Billing(id);
	}

	public void clickmoneyitem(GameObject btn)
	{
		showselect(0);
	}

	public void clickdiamonditem(GameObject btn)
	{
		showselect(1);
	}

	public void clickenergyitem(GameObject btn)
	{
		showselect(2);
	}

	public void showselect(int num)
	{
		for (int i = 0; i < selectfors.Length; i++)
		{
			if (num == i)
			{
				selectfors[i].enabled = true;
				NGUITools.SetActive(chargeitemobj[i], state: true);
			}
			else
			{
				selectfors[i].enabled = false;
				NGUITools.SetActive(chargeitemobj[i], state: false);
			}
		}
		if (num == 2)
		{
			isenergy = true;
			if (MenuPlayerprefs.instance.energylimittimes >= 3)
			{
				item3wi.alpha = 0f;
				item3wi.gameObject.SetActive(false);
				item1an.runOnlyOnce = true;
				item1an.relativeOffset.x = -0.165f;
				item1an.enabled = true;
				item2an.runOnlyOnce = true;
				item2an.relativeOffset.x = 0.165f;
				item2an.enabled = true;
			}
			else
			{
				item3wi.alpha = 1f;
				item3wi.gameObject.SetActive(true);
				item1an.runOnlyOnce = true;
				item1an.relativeOffset.x = -0.33f;
				item1an.enabled = true;
				item2an.runOnlyOnce = true;
				item2an.relativeOffset.x = 0f;
				item2an.enabled = true;
				limittimesflag.spriteName = "CZ_" + MenuPlayerprefs.instance.energylimittimes + "_3";
			}
		}
		else
		{
			isenergy = false;
		}
	}

	public void clickclose(GameObject btn)
	{
		disable();
		MenuBackController.instance.BackMenuUIstate();
	}

	public void enable(int num)
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		curitem = num;
		init_ui_info();
	}

	public void disable()
	{
		NGUITools.SetActive(uiobj, state: false);
	}
}
